package com.material.demo.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;

import com.material.demo.R;
import com.material.demo.lyric.LyricInfo;
import com.material.demo.lyric.LyricView;
import com.material.demo.lyric.ZrceParser;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by pc on 2016/12/27.
 */

public class LyricViewActivity extends DemoBaseActivity implements View.OnClickListener {

    private LyricView lyricView;
    private ZrceParser parser;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lyricview);

        lyricView = (LyricView) findViewById(R.id.lyric);


        try {
            InputStream stream = getResources().getAssets().open("2888.lrcx");
            parser = new ZrceParser();
            parser.parser(stream);

            lyricView.setLyricParser(parser);
            lyricView.setBackgroundColor(Color.RED);
            lyricView.setLyricLines(5,3);
            lyricView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    lyricView.start();
                }
            },50);
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("Chat","  "+ e.toString());
        }

        findViewById(R.id.btn_next).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(parser!=null && parser.getLyrics()!=null){
            int index = lyricView.getCurLineIndex();
            if(index < parser.getLyrics().size()-1){
                LyricInfo info = (LyricInfo) parser.getLyrics().get(index+1);
                lyricView.updateLyric(info.startTime+1);
            }
        }
    }
}
